package com.ch.android.lookforkotlinv2.activity

import android.Manifest
import android.os.Bundle
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.DataBindingUtil
import androidx.fragment.app.Fragment
import com.ch.android.lookforkotlinv2.R
import com.ch.android.lookforkotlinv2.adapter.HomePageAdapter
import com.ch.android.lookforkotlinv2.databinding.ActivityHomeBinding
import com.ch.android.lookforkotlinv2.fragment.HomeBlankFragment
import com.ch.android.lookforkotlinv2.fragment.PreviewFragment
import com.ch.android.lookforkotlinv2.fragment.SquareFragment
import com.ch.android.lookforkotlinv2.vm.HomeModel
import pub.devrel.easypermissions.AppSettingsDialog
import pub.devrel.easypermissions.EasyPermissions


class HomeActivity : AppCompatActivity() , EasyPermissions.PermissionCallbacks {
    private lateinit var mBinding: ActivityHomeBinding
    private lateinit var homeModel: HomeModel
    private val mFragments = mutableListOf<Fragment>()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        mBinding = DataBindingUtil.setContentView(this, R.layout.activity_home)
        mBinding.lifecycleOwner = this
        homeModel = HomeModel()
        mBinding.homeModel = homeModel

        mFragments.add(SquareFragment())
        mFragments.add(HomeBlankFragment.newInstance("B", "b"))
        mFragments.add(PreviewFragment())
        mFragments.add(HomeBlankFragment.newInstance("D", "d"))
        mFragments.add(HomeBlankFragment.newInstance("E", "e"))

        val pageAdapter = HomePageAdapter(this, mFragments)
        mBinding.homeViewPager.adapter = pageAdapter
        //禁止ViewPager2滑动
        mBinding.homeViewPager.isUserInputEnabled = false
/*
        //ViewPager2滑动监听
        mBinding.homeViewPager.registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() {
            override fun onPageSelected(position: Int) {
                mBinding.homeBottomNav.menu.getItem(position).isChecked = true
            }
        })
*/
        mBinding.homeBottomNav.setOnItemSelectedListener { item ->
            //需要在xml/bottom_nav_menu.xml中设置android:orderInCategory属性, 才可以使用item.order
            mBinding.homeViewPager.setCurrentItem(item.order, false)
/*
            when (item.itemId) {
                R.id.bottom_nav_square -> {
                    mBinding.homeViewPager.currentItem = 0
                }

                R.id.bottom_nav_nearby -> {
                    mBinding.homeViewPager.currentItem = 1
                }

                R.id.bottom_nav_live -> {
                    mBinding.homeViewPager.currentItem = 2
                }

                R.id.bottom_nav_hot -> {
                    mBinding.homeViewPager.currentItem = 3
                }

                R.id.bottom_nav_mine -> {
                    mBinding.homeViewPager.currentItem = 4
                }
            }
*/
            true
        }
        requestPermissions()
    }

    /**
     * 请求权限
     */
    private fun requestPermissions() {
        val perms = arrayOf(Manifest.permission.CAMERA, Manifest.permission.ACCESS_FINE_LOCATION)
        if (EasyPermissions.hasPermissions(this, *perms)) {
            // Already have permission, do the thing
            // ...
            Log.d("HomeActivity", "requestPermissions: Already have permission")
        } else {
            // Do not have permissions, request them now
            EasyPermissions.requestPermissions(this, "需要相机和定位权限", 0, *perms)
        }

    }

    override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<String>, grantResults: IntArray) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)
        // Forward results to EasyPermissions
        EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this)
    }

    override fun onPermissionsGranted(requestCode: Int, perms: MutableList<String>) {
        Log.d("HomeActivity", "onPermissionsGranted: $perms")
    }

    override fun onPermissionsDenied(requestCode: Int, perms: MutableList<String>) {
        Log.d("HomeActivity", "onPermissionsDenied: $perms")
        if (EasyPermissions.somePermissionPermanentlyDenied(this, perms)) {
//            AppSettingsDialog.Builder(this).build().show()
            AppSettingsDialog.Builder(this)
                .setTitle(R.string.permissions_required)
                .setRationale(R.string.permissions_required_rationale)
                .build().show()
        }

    }

}